function Uploader(target, callback, size) {
    var file, myUrl, fileSize, imageUrl, orientation, fileFilter;

    if (!target) return

    file = target
    fileSize = file.size / 1024 / 1024

    myUrl = window.URL || window.webkitURL
    imageUrl = myUrl.createObjectURL(file)

    // 限制上传图片大小
    if (size && fileSize > size) return alert("图片过大")

    // 限制图片格式
    fileFilter = /^(image\/jpeg|image\/jpg|image\/png)$/i
    if (file.type && !fileFilter.test(file.type)) return alert("图片格式不对")

    document.getElementById("loading").classList.remove("hide")

    EXIF.getData(file, () => {
        var newImage, expectWidth, expectHeight;

        // 获取照片方向角属性
        orientation = EXIF.getTag(this, 'Orientation')

        newImage = new Image()
        newImage.src = imageUrl
        newImage.onload = function () {
            expectWidth = this.naturalWidth
            expectHeight = this.naturalHeight

            // 修饰长宽比例
            if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
                expectWidth = 1000
                expectHeight = expectWidth * this.naturalHeight / this.naturalWidth
            } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
                expectHeight = 1400
                expectWidth = expectHeight * this.naturalWidth / this.naturalHeight
            }

            // 动态构建canvas标签
            var myCanvas, ctx, quality, base64;

            myCanvas = document.createElement('canvas')
            ctx = myCanvas.getContext('2d')

            // 设置宽高
            myCanvas.width = expectWidth
            myCanvas.height = expectHeight

            // 设置重量比例
            quality = 0.5

            // 将图片画在canvas上
            ctx.drawImage(this, 0, 0, expectWidth, expectHeight)

            // 修复ios
            if (navigator.userAgent.match(/(iPhone|iPad|iPod|iOS)/i)) {
                if (orientation != '' && orientation != 1) {
                    switch (orientation) {
                        case 6: // 需要顺时针（向左）90度旋转
                            rotateImg(this, 'left', myCanvas)
                            break
                        case 8: // 需要逆时针（向右）90度旋转
                            rotateImg(this, 'right', myCanvas)
                            break
                        case 3: // 需要180度旋转
                            rotateImg(this, 'right', myCanvas)
                            rotateImg(this, 'right', myCanvas)
                            break
                    }
                }
                base64 = myCanvas.toDataURL('image/jpeg', quality)
            } else if (navigator.userAgent.match(/Android/i)) {
                base64 = myCanvas.toDataURL('image/jpeg', quality)
            } else {
                if (orientation != '' && orientation != 1) {
                    switch (orientation) {
                        case 6:
                            rotateImg(this, 'left', myCanvas)
                            break
                        case 8:
                            rotateImg(this, 'right', myCanvas)
                            break
                        case 3:
                            rotateImg(this, 'right', myCanvas) // 转两次
                            rotateImg(this, 'right', myCanvas)
                            break
                    }
                }
                base64 = myCanvas.toDataURL('image/jpeg', quality)
            }

            document.getElementById("loading").classList.add("hide")

            callback({
                imageSrc: imageUrl, // 本地图片路径  未压缩的路径
                base64: base64, // 压缩的路径
                fileName: file.name || '' // 图片名
            })
        }
    })
}

function rotateImg(img, direction, canvas) {

    // 最小与最大旋转方向，图片旋转4次后回到原方向
    var minStep = 0,
        maxStep = 3,
        step = 2,
        _width, _height, degree, ctx;

    if (!img) return

    _width = img.width
    _height = img.height

    if (!step) step = minStep

    if (direction === 'right') {
        step++

        // 旋转超过最大值 等于最小值 轮播图
        step > maxStep && (step = minStep)
    } else {
        step--

        // 旋转超过最小值 等于最大值
        step < minStep && (step = maxStep)
    }

    // 旋转角度以弧度值为参数
    degree = step * Math.PI / 2
    ctx = canvas.getContext('2d')

    canvas.width = width
    canvas.height = height
    ctx.rotate(degree)

    switch (step) {
        case 0:
            ctx.drawImage(img, 0, 0)
            break
        case 1:
            ctx.drawImage(img, 0, -height)
            break
        case 2:
            ctx.drawImage(img, -width, -height)
            break
        case 3:
            ctx.drawImage(img, -width, 0)
    }
}

window.Uploader = Uploader